﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gedea.WinForm.Helpers;
using Gedea.Model.Entities;
using Gedea.WinForm.Resources.DBObjects;
using Gedea.Service.Services;

namespace Gedea.WinForm.Forms.Customers
{
    public partial class Search : Form
    {
        private delegate void Deleg1();
        private delegate void Deleg2(Customer cust);
        private List<Customer> customers = new CustomerService().List();
        private int currentId;

        #region Ctr
        public static Search Instance
        {
            get { return ViewLocator<Search>.Instance; }
        }

        private Search()
        {
            InitializeComponent();
        }
        #endregion

        private void Search_FormClosed(object sender, FormClosedEventArgs e)
        {
            ViewLocator<Search>.CloseInstance = null;
        }

        private void radioButtons_CheckedChanged(Object sender, EventArgs e)
        {
            RadioButton checkedButton = GetCurrentRadioButton();

            CustomizeForField(checkedButton.Text);
        }

        private RadioButton GetCurrentRadioButton()
        {
            return Instance.groupBox1.Controls.OfType<RadioButton>().FirstOrDefault(r => r.Checked);
        }

        private void CustomizeForField(string fieldString)
        {
            Gedea.Model.DBObjects.Customers field = new Model.DBObjects.Customers();
            List<Customer> data = new List<Customer>();

            if (fieldString == CustomerStrings.ResourceManager.GetString(Gedea.Model.DBObjects.Customers.realName.ToString()))
            {
                field = Gedea.Model.DBObjects.Customers.realName;
                data = customers.OrderBy(x => x.RealName).ToList();
            }
            else if (fieldString == CustomerStrings.ResourceManager.GetString(Gedea.Model.DBObjects.Customers.idForBank.ToString()))
            {
                field = Gedea.Model.DBObjects.Customers.idForBank;
                data = customers.OrderBy(x => x.IdForBank).ToList();
            }
            else if (fieldString == CustomerStrings.ResourceManager.GetString(Gedea.Model.DBObjects.Customers.cbu.ToString()))
            {
                field = Gedea.Model.DBObjects.Customers.cbu;
                data = customers.OrderBy(x => x.Cbu).ToList();
            }
            comboBox1.DisplayMember = field.ToString();
            comboBox1.DataSource = data;
        }

        private void Search_Load(object sender, EventArgs e)
        {
            UserControls.CustomerControl viewUser = new UserControls.CustomerControl(new Deleg1(CloseApp), new Deleg2(EditCustomer), true);
            panel1.Controls.Add(viewUser);

            RaddioButtons_Loads();

            ComboBox_Load(Gedea.Model.DBObjects.Customers.realName);

            BlockFields(false);

        }

        private void ComboBox_Load(Gedea.Model.DBObjects.Customers currentField)
        {
            //AutoCompleteStringCollection AutoComp = new AutoCompleteStringCollection();

            //textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            //textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            //IML
            //AutoComp.Add("Konzok, Gisela, 23.11.1938, R060354");
            //AutoComp.Add("Kopf, Alfred, 4.12.1929, R060075");
            //AutoComp.Add("Köper, Wilhelm, 23.11.1943, R060186");


            comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;

            CustomizeForField(GetCurrentRadioButton().Text);

            comboBox1.ValueMember = "CustomerId";

        }

        private void RaddioButtons_Loads()
        {
            radioButton1.Text = CustomerStrings.ResourceManager.GetString(Gedea.Model.DBObjects.Customers.realName.ToString());
            radioButton2.Text = CustomerStrings.ResourceManager.GetString(Gedea.Model.DBObjects.Customers.idForBank.ToString());
            radioButton3.Text = CustomerStrings.ResourceManager.GetString(Gedea.Model.DBObjects.Customers.cbu.ToString());
        }

        private void CloseApp()
        {
            this.Close();
        }

        private void EditCustomer(Customer cust)
        {
            cust.CustomerId = currentId;
            new CustomerService().Update(cust);
            MessageBox.Show("El Cliente ha sido modificado con éxito");
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Customer cust = customers.Where(x => x.CustomerId == Convert.ToInt32(comboBox1.SelectedValue)).FirstOrDefault();
            if (cust == null)
            {
                MessageBox.Show("No se encontró el cliente buscado");
            }
            else
            {
                currentId = Convert.ToInt32(comboBox1.SelectedValue);
                UserControls.CustomerControl control = (UserControls.CustomerControl)panel1.Controls[0];
                control.myControls[0].Text = cust.RealName;
                control.myControls[1].Text = cust.Cbu;
                control.myControls[2].Text = cust.IdForBank;
                ((ComboBox)control.myControls[3]).SelectedValue = cust.AreaId;
                BlockFields(true);
            }
        }

        private void BlockFields(bool resp)
        {
            UserControls.CustomerControl control = (UserControls.CustomerControl)panel1.Controls[0];
            control.myControls[0].Enabled = resp;
            control.myControls[1].Enabled = resp;
            control.myControls[2].Enabled = resp;
            control.myControls[3].Enabled = resp;
        }


    }
}
